/**
 * 用户登录框
 * @class LoginWindow
 * @extends Ext.Window
 */
LoginWindow = Ext.extend(Ext.Window, {
	id: 'login-win',
	layout: 'border',
    title: '请登录',
    width: 600,
    height: 270,
	collapsible : false,
	closable: false,
	draggable: false,
	resizable: false,
	frame: true,
	border: false,
	buttonAlign : 'right',
	iconCls: 'x-logon',
	logoPanel: null,
	formPanel: null,
    initComponent: function() {
    	
    	this.logoPanel = new Ext.Panel({
	 		baseCls: 'x-plain',
	 		id: 'login-logo',
	 		region: 'center'
 		});
 		
 		this.formPanel = new Ext.form.FormPanel({
 			id: 'login-form',
 			region: 'south',
 			baseCls: 'x-plain',
	 		baseParams: {
	 			module: 'login'
	 		},
            defaultType : 'textfield',
			//standardSubmit : true,
			method : 'POST',
			url: 'j_spring_security_check',
			border: false,
			labelWidth: 100,
			frame: false,
		 	height: 70,
			defaults : {
				allowBlank : false,
				msgTarget: 'side',
				width: 320
			},
			keys:[{
				key: Ext.EventObject.ENTER,
				fn: this.logon,
				scope: this
			}],
            items: [
                {
                    xtype: 'textfield',
                    id: 'j_username',
                    name : 'j_username',
					fieldLabel : '用户名',
                    value : 'admin',
					blankText : '用户名不能为空。'
                }, {
                    xtype: 'textfield',
                    id: 'j_password',
                    name : 'j_password',
                    value : 'admin',
					fieldLabel : '密 码',
					blankText : '密码不能为空。',
					inputType : 'password'
                }
            ],
            listeners: {
				'show': {
		        	fn: function() {
						this.findById("j_username").focus();
					},
		        	scope: this
		        }
			}
 		});
	 		
        this.items = [
        	this.logoPanel,
        	this.formPanel
        ];
        
        this.buttons = [
        	{
        		id : 'sure',
				text : '登录',
				type: 'submit',
				handler: this.logon,
				scope: this
			}
		]			
        LoginWindow.superclass.initComponent.call(this);
    },
    
    logon: function() {
    	var path = window.location.pathname;
 		path = path.substring(0, path.lastIndexOf('/') + 1);
 		path += "index.do";	
    	var form = this.formPanel.getForm();
    	if(form.isValid()) {
    		form.submit({
				clientValidation: false,
				waitMsg: '登录中，请稍等... ',
				waitTitle: '系统提示：',
				scope: this,
		    	success: function(form, action) {
		    		var path = window.location.pathname;
			 		path = path.substring(0, path.lastIndexOf('/') + 1);
			 		path += "index.do";	
			 		window.location = path;
		    	},
		    	failure: function(form, action){  
                	Ext.MessageBox.show({
                    	title: '提示信息',
                    	msg: action.result.error,
                    	buttons: Ext.MessageBox.OK,
                    	icon: Ext.MessageBox.ERROR
                	});
            	}
			});
    	}
    }
});

Ext.onReady(function() {
	Ext.QuickTips.init();
	var dlg = new LoginWindow();
	dlg.show();
});